package com.wu.webflux6.sms;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

public class SmsCodeAuthenticationToken extends AbstractAuthenticationToken {

	private static final long serialVersionUID = 500L;
	private final Object principal;
	private Object credentials;
	Map<String, String> concurrentHashMap = new ConcurrentHashMap<>();

	public SmsCodeAuthenticationToken(Object principal, Object credentials) {
		super(null);
		this.principal = principal;
		this.credentials = credentials;
		this.setAuthenticated(false);
	}

	public SmsCodeAuthenticationToken(Object principal, Object credentials, Map<String, String> concurrentHashMap) {
		super(null);
		this.principal = principal;
		this.credentials = credentials;
		this.concurrentHashMap = concurrentHashMap;
		this.setAuthenticated(false);
	}

	public Map<String, String> getConcurrentHashMap() {
		return concurrentHashMap;
	}

	public void setConcurrentHashMap(Map<String, String> concurrentHashMap) {
		this.concurrentHashMap = concurrentHashMap;
	}

	public SmsCodeAuthenticationToken(Object principal, Object credentials,
			Collection<? extends GrantedAuthority> authorities) {
		super(authorities);
		this.principal = principal;
		this.credentials = credentials;
		super.setAuthenticated(true);
	}

	@Override
	public Object getCredentials() {
		return this.credentials;
	}

	@Override
	public Object getPrincipal() {
		return this.principal;
	}

	public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
		if (isAuthenticated) {
			throw new IllegalArgumentException(
					"Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
		} else {
			super.setAuthenticated(false);
		}
	}

	public void eraseCredentials() {
		super.eraseCredentials();
		credentials = null;
	}

}